package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.isunland.managesystem.adapter.MaterialMunitAdapter;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseOriginal;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.MaterialMunit;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/8/3 18:14
 */
public class MunitListFragment extends BaseListFragment {

  private ArrayList<MaterialMunit> mList;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom("物料单位");
    mList = new ArrayList<>();
  }

  @Override public String getUrl() {
    return ApiConst.URL_MATERIAL_MUNIT;
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("materialId", mBaseParams.getId());
    params.put("memberCode", mCurrentUser.getMemberCode());
    params.put("page", "1");
    params.put("rows", "1000");
    return params.getParams();
  }

  @Override public void resolveResponse(String json) {
    BaseOriginal<MaterialMunit> original =
        new Gson().fromJson(json, new TypeToken<BaseOriginal<MaterialMunit>>() {
        }.getType());
    //if (Base.SUCCESS == original.getResult() && original.getRows() != null) {
    mList.clear();
    mList.addAll(original.getRows());
    MaterialMunitAdapter madapter = new MaterialMunitAdapter(getActivity(), mList);
    setListAdapter(madapter);
    //} else {
    //  ToastUtil.show("获取列表失败");
    //}
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    MaterialMunit content = mList.get(position - 1);
    Intent intent = new Intent();
    intent.putExtra(BaseFragment.EXTRA_PARAMS, content);
    getActivity().setResult(Activity.RESULT_OK, intent);
    getActivity().finish();
  }
}
